<template>
    <div class="quteam-channel-detail" v-loading="loading">
        <div class="detail-top">
            <div class="input-box">
                <div>
                    <p class="detail-title">累计新增</p>
                    <p class="detail-txt">{{ channelDate.totalAdd }}</p>
                </div>
                <div>
                    <p class="detail-title">近30日平均新增</p>
                    <p class="detail-txt">{{ channelDate.monthAverageAdd }}</p>
                </div>
                <div>
                    <p class="detail-title">近30日活跃</p>
                    <p class="detail-txt">{{ channelDate.monthAverageActive }}</p>
                </div>
            </div>
            <el-date-picker
                v-model="date"
                clearable
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                @change="dateChange">
            </el-date-picker>
        </div>
        <div class="table-box">
            <el-table :data="channelDate.channelDataDetailEntryVoList" ref="table" border style="width: 100%">
                <el-table-column min-width="120" label="日期">
                    <template slot-scope="scope">
                        {{ tableFormatDate(scope.row.ymd) }}
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="新增用户" prop="newAdd">
                    <template slot-scope="scope">
                        <div v-if="scope.row.newAdd && scope.row.newAdd >= 0">{{ scope.row.newAdd }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="当日注册用户" prop="register">
                    <template slot-scope="scope">
                        <div v-if="scope.row.register && scope.row.register >= 0">{{ scope.row.register }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
                <el-table-column min-width="100" label="活跃用户" prop="active">
                    <template slot-scope="scope">
                        <div v-if="scope.row.active && scope.row.active >= 0">{{ scope.row.active }}</div>
                        <div v-else>0</div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'

export default {
    data () {
        return {
            pageNum: 1,
            pageSize: this._pageSize,
            paramsList: {},
            date: [new Date('2020-09-17').getTime(), new Date().getTime()],
            tableData: [],
            channelDate: {},
            pickerOptions: {
                disabledDate (time) {
                    return time.getTime() < new Date('2020-08-07 00:00:00').getTime()
                },
                shortcuts: [
                    {
                        text: '最近7天',
                        onClick (picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                            end.setTime(end.getTime() - 3600 * 1000 * 24)
                            picker.$emit('pick', [start, end])
                        }
                    },
                    {
                        text: '最近30天',
                        onClick (picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                            end.setTime(end.getTime() - 3600 * 1000 * 24)
                            picker.$emit('pick', [start, end])
                        }
                    }
                ]
            },
            total: 0,
            loading: false
        }
    },
    created () {
        this.paramsList.fromDate = formatDate(new Date('2020-09-17'), 'YYYY-MM-DD') + ' 00:00:00'
        this.paramsList.endDate = formatDate(new Date(), 'YYYY-MM-DD') + ' 23:59:59'
        this.getList(1)
    },
    methods: {
        // 获取列表
        async getList (pageNum) {
            if (this.loading) return
            this.pageNum = pageNum
            this.loading = true
            let params = {
                pageNum,
                pageSize: this.pageSize,
                channel: this.$route.query.channel
            }
            params = Object.assign(params, this.paramsList)
            let res = await this.$http.post('/team/selectChannelDetailData', params)
            if (res.code === '0') {
                this.channelDate = res.data
                this.total = res.count
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        // 查看用户详情
        goUserDetail (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },
        dateChange (val) {
            if (val) {
                this.paramsList.fromDate = formatDate(new Date(val[0]), 'YYYY-MM-DD') + ' 00:00:00'
                this.paramsList.endDate = formatDate(new Date(val[1]), 'YYYY-MM-DD') + ' 23:59:59'
            } else {
                this.paramsList.fromDate = formatDate(new Date('2020-09-17'), 'YYYY-MM-DD') + ' 00:00:00'
                this.paramsList.endDate = formatDate(new Date(), 'YYYY-MM-DD') + ' 23:59:59'
            }
            this.getList(1)
        },
        // 页码切换
        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getList(1)
        },
        // 页容切换
        pageChange (val) {
            this.pageNum = val
            this.getList(this.pageNum)
        },
        tableFormatDate (time) {
            return formatDate(time, 'YYYY-MM-DD HH:MM:SS')
        }
    }
}
</script>

<style lang="scss" scoped>
    .quteam-channel-detail {
        .detail-top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            .input-box {
                display: flex;
                margin-top: 10px;
                font-size: 13px;
                font-weight: 500;
                .input-title {
                    margin: 0 10px 0 20px;
                }
                .btn {
                    margin-top: 10px;
                    margin-left: 10px;
                }
                div {
                    margin-right: 30px;
                    margin-bottom: 20px;
                    .detail-title {
                        font-size: 12px;
                        color: #909399;
                    }
                    .detail-txt {
                        margin-top: 10px;
                        font-size: 18px;
                    }
                }
            }
        }
        .table-box {
            margin: 20px 0;
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }
    }
</style>
